﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

using HadronData;

namespace Hadron.Entities
{
    class Vent
        : LandEntity
    {
        private static Polygon s_polygonVisible;

        private float m_fInterval;
        private ParticleSystem m_particleSystem;
        private Edge m_edgeContact;
        private Polygon m_polygonVisible;
        private SoundEffectInstance m_soundEffectInstanceVent;

        public Vent(EntityMarker p_entityMarker, Environment p_environment)
            : base(p_entityMarker, p_environment)
        {
            if (s_polygonVisible == null)
            {
                s_polygonVisible = new Polygon();
                s_polygonVisible.Edges.Add(new Edge(-8.0f, 0.0f, -4.0f, -8.0f));
                s_polygonVisible.Edges.Add(new Edge(8.0f, 0.0f, 4.0f, -8.0f));
                s_polygonVisible.Edges.Add(new Edge(-4.0f, -8.0f, 4.0f, -8.0f));
                s_polygonVisible.UpdateBounds();
            }

            m_nHealth = 1;
            m_fInterval = 4.0f;
            m_particleSystem = new ParticleSystem(64, 64.0f, 1.0f);
            m_particleSystem.EmissionVelocityVariance = 32.0f;
            m_particleSystem.ParticleColours = new Color[] { Color.Cyan, Color.Orange, Color.Black };

            m_edgeContact = new Edge();

            m_polygonVisible = new Polygon(s_polygonVisible);
            m_polygonVisible.Transform(m_trnPlacement);
            m_polygonVisible.Colour = Color.DimGray;

            m_soundEffectInstanceVent
                = AssetManager.Sounds.Vent.Play(0.0f, 0.0f, 0.0f, true);
            m_soundEffectInstanceVent.Stop();

            m_circleBounds.Radius = 64.0f;
        }

        public override void Update(GameTime p_gameTime)
        {
            base.Update(p_gameTime);

            m_circleBounds.Centre = m_trnPlacement.Translation;

            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;

            m_fInterval -= fDeltaTime;
            if (m_fInterval <= 0.0f)
            {
                if (m_particleSystem.Active)
                {
                    m_particleSystem.Active = false;
                    m_soundEffectInstanceVent.Stop();
                    m_fInterval = 4.0f + (float) s_random.NextDouble();
                }
                else
                {
                    m_particleSystem.Active = true;
                    m_soundEffectInstanceVent.Resume();
                    m_fInterval = 2.0f + (float)s_random.NextDouble();
                }
            }

            m_polygonVisible.Transform(m_trnPlacement, s_polygonVisible);

            Vector2 vecPerp = m_trnPlacement.RotationComponent.Apply(new Vector2(0.0f, -1.0f));
            m_particleSystem.Position = Position + vecPerp * 8.0f;
            m_particleSystem.EmissionVelocity = vecPerp * 64.0f;
            m_particleSystem.Update(p_gameTime);

            m_edgeContact.Start = Vector2.Zero;
            if (m_particleSystem.Active)
            {
                m_edgeContact.End = new Vector2(0.0f, -64.0f);
                m_soundEffectInstanceVent.Volume = m_environment.ComputeSoundVolume(Position);
            }
            else
                m_edgeContact.End = Vector2.Zero;
            m_edgeContact.Transform(m_trnPlacement);
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            m_polygonVisible.Draw(p_spriteBatch);
            m_particleSystem.Draw(p_spriteBatch);
        }

        public override void OnDeactivate()
        {
            m_soundEffectInstanceVent.Stop();
        }

        public override EntityType Type
        {
            get { return EntityType.Vent; }
        }

        public override bool Hostile
        {
            get { return true; }
        }

        public override bool Destructuble
        {
            get { return false; }
        }

        public override Geometry Geometry
        {
            get { return m_edgeContact; }
        }
    }
}
